package com.example.administrator.myapplication;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

public class StoreDetialActivity extends Activity {
    private String store_name;
    private String store_sn;
    private TextView store_status;
    private String partner_name;
    private String mobile;
    private String equip_type;
    private String equip_code;
    private String address;
    private  int equip_id;
    private Handler mHandler;
    private TextView store_status_textview, store_sn_textview, store_name_textview, partner_name_textview, mobile_textview, equip_type_textview,equip_code_textview,address_textview;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_storedetial);
        super.onCreate(savedInstanceState);
        sharedPreferences();
        httpContect();
        handlerGetData();
        initView();


    }

    public void httpContect() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpContect httpContect = new HttpContect();

                String route = "api/store/my-store";
                String data = "access_token=" + sharedPreferences();
                String JsonValueReselution = httpContect.send(route, data);

                Looper.prepare();
                Message message = new Message();
                message.obj = JsonValueReselution;
                mHandler.sendMessage(message);
                Looper.loop();

            }
        }).start();

    }

    /*
     *
     * TODO:获取本地token
     * */
    private String sharedPreferences() {
        SharedPreferences sharedPreferences = getSharedPreferences("data", 0);
        final String access_token = sharedPreferences.getString("access_token", null);
        return access_token;
    }

    /*
     * TODO:初始化
     *
     * */
    private void initView() {
        store_name_textview = findViewById(R.id.store_name);
        store_sn_textview = findViewById(R.id.store_sn);
        store_status_textview = findViewById(R.id.store_status);
        partner_name_textview = findViewById(R.id.partner_name);
        mobile_textview = findViewById(R.id.mobile);
        equip_type_textview = findViewById(R.id.equip_type);
        equip_code_textview = findViewById(R.id.equip_code);
        address_textview = findViewById(R.id.address);


    }

    /*
     *
     *
     * TODO:Message传递数据
     *
     * */
    private void handlerGetData() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                String getInformation = (String) msg.obj;
                jsonResolve(getInformation);
                Log.i("123231", "~~~~~~~~~~~~@@@@@@@@@" + getInformation);
                super.handleMessage(msg);
            }
        };


    }

    /*
        TODO:获取json数据解析


    */
    private void jsonResolve(String getInformation) {
        try {
            JSONObject object = new JSONObject(getInformation);
            int error = object.optInt("error");
            if (error == 0) {
                JSONObject data = object.getJSONObject("data");
                store_name = data.optString("store_name");
                store_sn = data.optString("store_sn");
                int store_status = data.optInt("store_status");
                if (store_status == 1) {
                    this.store_status_textview.setText("已验证");
                }
                equip_type = data.optString("equip_type");
                equip_code = data.optString("equip_code");
               equip_id = data.optInt("equip_id");
                address = data.optString("address");
                partner_name = data.optString("partner_name");
                mobile = data.optString("mobile");
                jsonAssignment();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /*
     * TODO:展示到控件中
     *
     * */
    private void jsonAssignment() {
            store_name_textview.setText(store_name);
            store_sn_textview.setText(store_sn);
            equip_code_textview.setText(equip_code);
            equip_type_textview.setText(equip_type);
            address_textview.setText(address);
            partner_name_textview.setText(partner_name);
            mobile_textview.setText(mobile);

    }


}
